/*
 * Copyright (c) 2018.
 * http://www.ulabcare.com
 */

package com.mt.roll.gateway.config;


import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.cors.reactive.CorsUtils;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

/**
 * 路由配置
 *
 * @author jiff
 * @date 2018/11/1
 * @since 1.0
 */
@Configuration
@Slf4j
public class RouteConfig {
    private static final String ALLOWED_HEADERS = "Content-Type, authorization, credential, token, X-Requested-With";
    private static final String ALLOWED_METHODS = "*";
    private static final String ALLOWED_ORIGIN = "*";
    private static final String ALLOWED_EXPOSE = "*";
    private static final String MAX_AGE = "18000L";

    // @Bean
    // public WebFilter corsFilter() {
    //     return (ServerWebExchange exchange, WebFilterChain chain) -> {
    //         ServerHttpRequest request = exchange.getRequest();
    //         if (CorsUtils.isCorsRequest(request)) {
    //             ServerHttpResponse response = exchange.getResponse();
    //             HttpHeaders headers = response.getHeaders();
    //             headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, ALLOWED_ORIGIN);
    //             headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, ALLOWED_METHODS);
    //             headers.add(HttpHeaders.ACCESS_CONTROL_MAX_AGE, MAX_AGE);
    //             headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, ALLOWED_HEADERS);
    //             headers.add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, ALLOWED_EXPOSE);
    //             headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
    //             if (request.getMethod() == HttpMethod.OPTIONS) {
    //                 response.setStatusCode(HttpStatus.OK);
    //                 return Mono.empty();
    //             }
    //         }
    //         return chain.filter(exchange);
    //     };
    // }
}
